package com.sky.orangehireserver.modules.job_seeker.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sky.orangehireserver.aspect.ApiOperationLog;
import com.sky.orangehireserver.modules.job_seeker.dto.SeekerJobQueryDTO;
import com.sky.orangehireserver.modules.job_seeker.service.SeekerJobService;
import com.sky.orangehireserver.modules.job_seeker.vo.SeekerJobBriefVO;
import com.sky.orangehireserver.modules.job_seeker.vo.SeekerJobVO;
import com.sky.orangehireserver.utils.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 求职者岗位查询控制器
 * 
 * @author sky
 */
@Slf4j
@RestController
@RequestMapping("/seeker/jobs")
@RequiredArgsConstructor
@Tag(name = "求职者岗位查询", description = "求职者查看岗位相关接口")
public class SeekerJobController {

    private final SeekerJobService seekerJobService;

    /**
     * 分页查询岗位列表
     * 
     * @param queryDTO 查询条件DTO
     * @return 分页岗位信息
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询岗位列表", description = "根据条件分页查询岗位列表")
    @ApiOperationLog(description = "分页查询岗位列表")
    public Response<IPage<SeekerJobBriefVO>> getJobPage(@Valid SeekerJobQueryDTO queryDTO) {
        IPage<SeekerJobBriefVO> jobPage = seekerJobService.getJobPage(queryDTO);
        return Response.success(jobPage);
    }

    /**
     * 获取岗位详情
     * 
     * @param id 岗位ID
     * @return 岗位详细信息VO
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取岗位详情", description = "根据ID获取岗位详细信息")
    @ApiOperationLog(description = "获取岗位详情")
    public Response<SeekerJobVO> getJobById(@Parameter(description = "岗位ID") @PathVariable Long id) {
        SeekerJobVO jobVO = seekerJobService.getJobById(id);
        return Response.success(jobVO);
    }

    /**
     * 获取热门岗位列表
     * 
     * @param limit 数量限制
     * @return 热门岗位列表
     */
    @GetMapping("/hot")
    @Operation(summary = "获取热门岗位列表", description = "获取浏览量和投递量较高的热门岗位")
    @ApiOperationLog(description = "获取热门岗位列表")
    public Response<List<SeekerJobBriefVO>> getHotJobs(
            @Parameter(description = "数量限制") @RequestParam(defaultValue = "10") Integer limit) {
        List<SeekerJobBriefVO> hotJobs = seekerJobService.getHotJobs(limit);
        return Response.success(hotJobs);
    }

    /**
     * 获取最新岗位列表
     * 
     * @param limit 数量限制
     * @return 最新岗位列表
     */
    @GetMapping("/latest")
    @Operation(summary = "获取最新岗位列表", description = "获取最新发布的岗位")
    @ApiOperationLog(description = "获取最新岗位列表")
    public Response<List<SeekerJobBriefVO>> getLatestJobs(
            @Parameter(description = "数量限制") @RequestParam(defaultValue = "10") Integer limit) {
        List<SeekerJobBriefVO> latestJobs = seekerJobService.getLatestJobs(limit);
        return Response.success(latestJobs);
    }

    /**
     * 根据企业ID获取岗位列表
     * 
     * @param orgId 企业ID
     * @param current 当前页
     * @param size 每页大小
     * @return 分页岗位信息
     */
    @GetMapping("/organization/{orgId}")
    @Operation(summary = "根据企业ID获取岗位列表", description = "获取指定企业的所有开放岗位")
    @ApiOperationLog(description = "根据企业ID获取岗位列表")
    public Response<IPage<SeekerJobBriefVO>> getJobsByOrgId(
            @Parameter(description = "企业ID") @PathVariable Long orgId,
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Long size) {
        IPage<SeekerJobBriefVO> jobPage = seekerJobService.getJobsByOrgId(orgId, current, size);
        return Response.success(jobPage);
    }

    /**
     * 根据城市获取岗位列表
     * 
     * @param location 城市
     * @param current 当前页
     * @param size 每页大小
     * @return 分页岗位信息
     */
    @GetMapping("/location/{location}")
    @Operation(summary = "根据城市获取岗位列表", description = "获取指定城市的所有开放岗位")
    @ApiOperationLog(description = "根据城市获取岗位列表")
    public Response<IPage<SeekerJobBriefVO>> getJobsByLocation(
            @Parameter(description = "城市") @PathVariable String location,
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Long size) {
        IPage<SeekerJobBriefVO> jobPage = seekerJobService.getJobsByLocation(location, current, size);
        return Response.success(jobPage);
    }

    /**
     * 根据标签获取岗位列表
     * 
     * @param tag 标签
     * @param current 当前页
     * @param size 每页大小
     * @return 分页岗位信息
     */
    @GetMapping("/tag/{tag}")
    @Operation(summary = "根据标签获取岗位列表", description = "获取包含指定标签的所有开放岗位")
    @ApiOperationLog(description = "根据标签获取岗位列表")
    public Response<IPage<SeekerJobBriefVO>> getJobsByTag(
            @Parameter(description = "标签") @PathVariable String tag,
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Long current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Long size) {
        IPage<SeekerJobBriefVO> jobPage = seekerJobService.getJobsByTag(tag, current, size);
        return Response.success(jobPage);
    }

    /**
     * 增加岗位浏览次数
     * 
     * @param id 岗位ID
     * @return 操作结果
     */
    @PutMapping("/{id}/view")
    @Operation(summary = "增加岗位浏览次数", description = "记录岗位被浏览")
    @ApiOperationLog(description = "增加岗位浏览次数")
    public Response<Void> incrementViewsCount(@Parameter(description = "岗位ID") @PathVariable Long id) {
        seekerJobService.incrementViewsCount(id);
        return Response.success();
    }

    /**
     * 获取岗位统计信息
     * 
     * @return 统计信息
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取岗位统计信息", description = "获取岗位的统计数据")
    @ApiOperationLog(description = "获取岗位统计信息")
    public Response<Map<String, Object>> getJobStatistics() {
        Map<String, Object> statistics = seekerJobService.getJobStatistics();
        return Response.success(statistics);
    }

    /**
     * 获取热门搜索关键词
     * 
     * @param limit 数量限制
     * @return 热门关键词列表
     */
    @GetMapping("/hot-keywords")
    @Operation(summary = "获取热门搜索关键词", description = "获取用户搜索频率较高的关键词")
    @ApiOperationLog(description = "获取热门搜索关键词")
    public Response<List<String>> getHotSearchKeywords(
            @Parameter(description = "数量限制") @RequestParam(defaultValue = "10") Integer limit) {
        List<String> hotKeywords = seekerJobService.getHotSearchKeywords(limit);
        return Response.success(hotKeywords);
    }

    /**
     * 获取推荐岗位
     * 
     * @param userId 用户ID（从请求头或上下文中获取）
     * @param limit 数量限制
     * @return 推荐岗位列表
     */
    @GetMapping("/recommended")
    @Operation(summary = "获取推荐岗位", description = "基于用户浏览历史等获取个性化推荐岗位")
    @ApiOperationLog(description = "获取推荐岗位")
    public Response<List<SeekerJobBriefVO>> getRecommendedJobs(
            @Parameter(description = "用户ID", hidden = true) @RequestHeader(value = "X-User-Id", required = false) Long userId,
            @Parameter(description = "数量限制") @RequestParam(defaultValue = "10") Integer limit) {
        
        // 如果没有用户ID，返回热门岗位作为推荐
        List<SeekerJobBriefVO> recommendedJobs = seekerJobService.getRecommendedJobs(userId, limit);
        return Response.success(recommendedJobs);
    }
}
